package com.company.cloud.gateway.component.route;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.util.StringUtils;

/***/
public abstract class AbstractDynRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {

	private ZuulProperties properties;

	public AbstractDynRouteLocator(String servletPath, ZuulProperties properties) {
		super(servletPath, properties);
		this.properties = properties;
	}

	/**
	 * 
	 * */
	@Override
	public void refresh() {
		doRefresh();
	}

	@Override
	protected Map<String, ZuulRoute> locateRoutes() {
		LinkedHashMap<String, ZuulRoute> routesMap=new LinkedHashMap<String,ZuulRoute>();
		routesMap.putAll(super.locateRoutes()); //静态路由
		routesMap.putAll(loadDynamicRoute()); //动态路由
		// 优化一下配置
        LinkedHashMap<String, ZuulRoute> values = new LinkedHashMap<>();
        for (Map.Entry<String, ZuulRoute> entry : routesMap.entrySet()) {
            String path = entry.getKey();
            // Prepend with slash if not already present.
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            if (StringUtils.hasText(this.properties.getPrefix())) {
                path = this.properties.getPrefix() + path;
                if (!path.startsWith("/")) {
                    path = "/" + path;
                }
            }
            values.put(path, entry.getValue());
        }
        return values;
	}

	/**
	 * 加载路由配置，由子类去实现
	 */
	public abstract Map<String, ZuulRoute> loadDynamicRoute();

}
